{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": 1,
   "source": "import numpy as np",
   "id": "initial_id"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:04.158180Z",
     "start_time": "2025-11-02T08:14:04.153183Z"
    }
   },
   "cell_type": "code",
   "source": "A = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]",
   "id": "3db00cb85c820ccb",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:05.876391Z",
     "start_time": "2025-11-02T08:14:05.864883Z"
    }
   },
   "cell_type": "code",
   "source": "A",
   "id": "d1253989a246d879",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:11.298996Z",
     "start_time": "2025-11-02T08:14:11.295483Z"
    }
   },
   "cell_type": "code",
   "source": "type(A)",
   "id": "d5c07aeb440fecb1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:36.357154Z",
     "start_time": "2025-11-02T08:14:36.344647Z"
    }
   },
   "cell_type": "code",
   "source": "# list_to_maxtrix",
   "id": "3c6eb14fd445468d",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:45.637862Z",
     "start_time": "2025-11-02T08:14:45.630360Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 = np.array(A)",
   "id": "69557602f247187",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:49.533336Z",
     "start_time": "2025-11-02T08:14:49.518707Z"
    }
   },
   "cell_type": "code",
   "source": "arr1",
   "id": "f7d76c1f542e23d0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:14:53.939945Z",
     "start_time": "2025-11-02T08:14:53.928980Z"
    }
   },
   "cell_type": "code",
   "source": "type(arr1)",
   "id": "baf10a422ddc4ff1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:15:14.255245Z",
     "start_time": "2025-11-02T08:15:14.245738Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"A=\",A)",
   "id": "4fcf0b5dec3013cf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A= [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:15:34.722035Z",
     "start_time": "2025-11-02T08:15:34.710008Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"arr1=\",arr1)",
   "id": "cac2e471a9e01f74",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1= [[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:15:56.337999Z",
     "start_time": "2025-11-02T08:15:56.328494Z"
    }
   },
   "cell_type": "code",
   "source": "B = ((1,2,3,4),(5,6,7,8),(9,10,11,12))",
   "id": "187026be3a35c65e",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:15:59.316755Z",
     "start_time": "2025-11-02T08:15:59.310749Z"
    }
   },
   "cell_type": "code",
   "source": "B",
   "id": "87b1586952a9d0e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:16:05.247881Z",
     "start_time": "2025-11-02T08:16:05.241378Z"
    }
   },
   "cell_type": "code",
   "source": "arr2 = np.array(B)",
   "id": "8c48efe6bc8556ab",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:16:08.801170Z",
     "start_time": "2025-11-02T08:16:08.782157Z"
    }
   },
   "cell_type": "code",
   "source": "arr2",
   "id": "13be427ff2ef026e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:16:17.152413Z",
     "start_time": "2025-11-02T08:16:17.137256Z"
    }
   },
   "cell_type": "code",
   "source": "B",
   "id": "d14f2b77967da00c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:28:36.630075Z",
     "start_time": "2025-11-02T08:28:36.625569Z"
    }
   },
   "cell_type": "code",
   "source": "type(A)",
   "id": "bd2d9f46f2544575",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:28:42.706726Z",
     "start_time": "2025-11-02T08:28:42.694221Z"
    }
   },
   "cell_type": "code",
   "source": "type(B)",
   "id": "75ae8874cf835b31",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:28:46.738776Z",
     "start_time": "2025-11-02T08:28:46.726369Z"
    }
   },
   "cell_type": "code",
   "source": "type(arr1)",
   "id": "95693603731d9203",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:28:51.332938Z",
     "start_time": "2025-11-02T08:28:51.328570Z"
    }
   },
   "cell_type": "code",
   "source": "type(arr2)",
   "id": "e58c7b0871370e1b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:29:23.439822Z",
     "start_time": "2025-11-02T08:29:23.425670Z"
    }
   },
   "cell_type": "code",
   "source": "arr1.shape",
   "id": "92bf743f419881bf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:29:28.289224Z",
     "start_time": "2025-11-02T08:29:28.276378Z"
    }
   },
   "cell_type": "code",
   "source": "arr2.shape",
   "id": "4c8aced602196ebb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:31:37.326284Z",
     "start_time": "2025-11-02T08:31:37.314777Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# create class person\n",
    "class person:\n",
    "    def __init__(self,name,age):\n",
    "       self.name=name\n",
    "       self.age=age\n",
    "       \n",
    "    def speak(self):\n",
    "        return \"Hello my name is \"+self.name+\" and I am \"+self.age\n",
    "    \n",
    "    def work(self,work):\n",
    "        return \"my work is \"+work"
   ],
   "id": "ecae0e5bfc79d854",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:36:11.556487Z",
     "start_time": "2025-11-02T08:36:11.547191Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class student(person):\n",
    "    \n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "        \n",
    "    def speak(self,name,age):\n",
    "        return \"Hello my name is \"+self.name+\" and I am \"+self.age\n",
    "    \n",
    "    def work(self,work):\n",
    "        return \"my work is \"+work"
   ],
   "id": "e84b9b4817c5b2e0",
   "outputs": [],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:36:12.485797Z",
     "start_time": "2025-11-02T08:36:12.480148Z"
    }
   },
   "cell_type": "code",
   "source": "s1 = student('xiaoming',19)",
   "id": "37737247df06236c",
   "outputs": [],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:36:14.255531Z",
     "start_time": "2025-11-02T08:36:14.245510Z"
    }
   },
   "cell_type": "code",
   "source": "s1.work('PM')",
   "id": "4b9e8201bb26bccf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'my work is PM'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:36:14.852590Z",
     "start_time": "2025-11-02T08:36:14.838855Z"
    }
   },
   "cell_type": "code",
   "source": "s1.speak('xiaomi',20)",
   "id": "6e236b424fef7c4f",
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[35], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43ms1\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mspeak\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mxiaomi\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m20\u001B[39;49m\u001B[43m)\u001B[49m\n",
      "Cell \u001B[1;32mIn[32], line 8\u001B[0m, in \u001B[0;36mstudent.speak\u001B[1;34m(self, name, age)\u001B[0m\n\u001B[0;32m      7\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mspeak\u001B[39m(\u001B[38;5;28mself\u001B[39m,name,age):\n\u001B[1;32m----> 8\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mHello my name is \u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mname\u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m and I am \u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mage\u001B[49m\n",
      "\u001B[1;31mTypeError\u001B[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:37:41.093094Z",
     "start_time": "2025-11-02T08:37:41.079443Z"
    }
   },
   "cell_type": "code",
   "source": "## Numpy 利用np.random.random()生成矩阵中指定范围的浮点数和整数",
   "id": "dfc3cfa256b41eec",
   "outputs": [],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:39:52.654511Z",
     "start_time": "2025-11-02T08:39:52.644955Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## np.random.random(d0,d1,....dn)生成值为[0,1)区间的n维浮点数组\n",
    "## np.random.random(d0,d1)创建d0*d1阶矩阵"
   ],
   "id": "2fb0a60705924416",
   "outputs": [],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:40:00.874815Z",
     "start_time": "2025-11-02T08:40:00.863760Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "ecd167673515c52",
   "outputs": [],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:40:34.020041Z",
     "start_time": "2025-11-02T08:40:34.010530Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 = np.random.random((2,3))",
   "id": "b430863c9e69cc5a",
   "outputs": [],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:40:37.060090Z",
     "start_time": "2025-11-02T08:40:37.042533Z"
    }
   },
   "cell_type": "code",
   "source": "arr1",
   "id": "6428821bee476bd7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.68011849, 0.69269146, 0.65812279],\n",
       "       [0.53862334, 0.89247658, 0.07514771]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:42:49.002408Z",
     "start_time": "2025-11-02T08:42:48.999410Z"
    }
   },
   "cell_type": "code",
   "source": "## np.random.randint(low,high=None,size=None,dtype='I')",
   "id": "192d3de5c5478ef",
   "outputs": [],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:44:19.450601Z",
     "start_time": "2025-11-02T08:44:19.430333Z"
    }
   },
   "cell_type": "code",
   "source": "arr2 = np.random.randint(3,30,size=[2,3])",
   "id": "23f714ee43ef813a",
   "outputs": [],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:45:24.877337Z",
     "start_time": "2025-11-02T08:45:24.862380Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"生成维度为2X3阶的矩阵，值在3到30之间，不包含30：\",arr2)",
   "id": "a1419ae570dc2545",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成维度为2X3阶的矩阵，值在3到30之间，不包含30： [[19  7 16]\n",
      " [22  7 28]]\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:45:29.510229Z",
     "start_time": "2025-11-02T08:45:29.505992Z"
    }
   },
   "cell_type": "code",
   "source": "arr2",
   "id": "2344f4eae99d0730",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19,  7, 16],\n",
       "       [22,  7, 28]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (3) reshape()的使用，修改矩阵的形状",
   "id": "e9c5e9703d5cd112"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:49:58.235977Z",
     "start_time": "2025-11-02T08:49:58.219463Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A= [1,2,3,4,5,6]\n",
    "B= np.array(A)"
   ],
   "id": "204d33f6ad2ea9d3",
   "outputs": [],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:49:59.742859Z",
     "start_time": "2025-11-02T08:49:59.730793Z"
    }
   },
   "cell_type": "code",
   "source": "A",
   "id": "30d09026655412be",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:50:02.363972Z",
     "start_time": "2025-11-02T08:50:02.345890Z"
    }
   },
   "cell_type": "code",
   "source": "B",
   "id": "47e88f5f71988a2d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:50:08.898290Z",
     "start_time": "2025-11-02T08:50:08.882222Z"
    }
   },
   "cell_type": "code",
   "source": "type(A)",
   "id": "a19549319341b20f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:50:12.087061Z",
     "start_time": "2025-11-02T08:50:12.081534Z"
    }
   },
   "cell_type": "code",
   "source": "type(B)",
   "id": "4c3ba3befd23fcd7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:50:26.012001Z",
     "start_time": "2025-11-02T08:50:26.004998Z"
    }
   },
   "cell_type": "code",
   "source": "C1=B.reshape(2,3)  ## 2行列",
   "id": "f16eed1a8c9ac3ff",
   "outputs": [],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:50:28.611692Z",
     "start_time": "2025-11-02T08:50:28.604375Z"
    }
   },
   "cell_type": "code",
   "source": "C1",
   "id": "6bfb0dea06ea0d22",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:51:27.780297Z",
     "start_time": "2025-11-02T08:51:27.773784Z"
    }
   },
   "cell_type": "code",
   "source": "C2=B.reshape(3,2)  ##3行2列",
   "id": "9e39c6a50359750e",
   "outputs": [],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:51:28.238873Z",
     "start_time": "2025-11-02T08:51:28.229373Z"
    }
   },
   "cell_type": "code",
   "source": "C2",
   "id": "e6104bdaf47668b9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " ## 3. 矩阵元素的存取操作",
   "id": "24c8ad2f0c7abd2e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:53:53.144294Z",
     "start_time": "2025-11-02T08:53:53.129777Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C1的第0行元素C1[0]：\",C1[0])",
   "id": "f22de245b2c4d175",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C1的第0行元素C1[0]： [1 2 3]\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:54:32.150145Z",
     "start_time": "2025-11-02T08:54:32.143638Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C1的前2行元素C1[0:2]：\",C1[0:2])",
   "id": "1bbe141eb2a1e83c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C1的前2行元素C1[0:2]： [[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:57:34.919123Z",
     "start_time": "2025-11-02T08:57:34.908862Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C2的第0行和第2行元素C2[[0,2]]：\",C2[[0,2]])",
   "id": "bc47e833e007b407",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C2的第0行和第2行元素C2[[0,2]]： [[1 2]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T08:58:25.502648Z",
     "start_time": "2025-11-02T08:58:25.490134Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C2的第0行第2列元素C2[0,1]：\",C2[0,1])",
   "id": "975b51c2b6c356c2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C2的第0行和第2行元素C2[0,1]： 2\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:02:27.039247Z",
     "start_time": "2025-11-02T09:02:27.023047Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C1的第2列元素C1[:,1]：\",C1[:,1])",
   "id": "6fe2b5048cbfc6fc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C1的第2列元素C1[:,1]： [2 5]\n"
     ]
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:02:54.464603Z",
     "start_time": "2025-11-02T09:02:54.445815Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C2的第2列元素C2[:,1]：\",C2[:,1])",
   "id": "e0c38adf5aa6c970",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C2的第2列元素C2[:,1]： [2 4 6]\n"
     ]
    }
   ],
   "execution_count": 66
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 创建的创建",
   "id": "9a2ff62e9f6230b2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. 直接创建",
   "id": "a123a62fba8ffbce"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2.随机生成向量元素",
   "id": "1a6de3672fe7c6d3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.通过reshape()生成向量",
   "id": "1fe5c5686302311a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 特殊矩阵",
   "id": "d8b44d52d8ca8005"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1.零矩阵",
   "id": "f0abaf365f8c2ca6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:27:23.316485Z",
     "start_time": "2025-11-02T09:27:23.312498Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.zeros(10)\n",
    "arr2 = np.zeros((3,4))\n",
    "arr3 = np.array([np.zeros(10)])"
   ],
   "id": "1bee7555c6285ef",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:28:21.702803Z",
     "start_time": "2025-11-02T09:28:21.694481Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 # 全为0为元素个数为10的列表",
   "id": "fe5a8f7d8dd67a80",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:28:23.044865Z",
     "start_time": "2025-11-02T09:28:23.039840Z"
    }
   },
   "cell_type": "code",
   "source": "arr2 # 3行4列的0元素的矩阵",
   "id": "cbbee0203237a581",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:30:48.393537Z",
     "start_time": "2025-11-02T09:30:48.379032Z"
    }
   },
   "cell_type": "code",
   "source": "arr3",
   "id": "104cdda40462d2f0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:31:01.466188Z",
     "start_time": "2025-11-02T09:31:01.458186Z"
    }
   },
   "cell_type": "code",
   "source": "type(arr3)",
   "id": "32a23be7a2d6e25",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:32:14.702186Z",
     "start_time": "2025-11-02T09:32:14.694927Z"
    }
   },
   "cell_type": "code",
   "source": "np.shape(arr1)",
   "id": "b4e54e9e2bd65538",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:32:18.159109Z",
     "start_time": "2025-11-02T09:32:18.143127Z"
    }
   },
   "cell_type": "code",
   "source": "np.shape(arr2)",
   "id": "bb98f63985476c42",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:32:21.420090Z",
     "start_time": "2025-11-02T09:32:21.415038Z"
    }
   },
   "cell_type": "code",
   "source": "np.shape(arr3)",
   "id": "18082e81c8c973b8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 10)"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "dbe66755449d7ec2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2.方阵",
   "id": "8682706a247c8206"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:41:13.789278Z",
     "start_time": "2025-11-02T09:41:13.773121Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建元素为1-16随机整数的三阶方阵：\n",
    "import numpy as np\n",
    "arr = np.random.randint(1,16,[3,3])\n",
    "arr"
   ],
   "id": "6d6820a273c32017",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  9,  3],\n",
       "       [ 4,  2, 14],\n",
       "       [14, 11,  5]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:41:25.691117Z",
     "start_time": "2025-11-02T09:41:25.687611Z"
    }
   },
   "cell_type": "code",
   "source": "type(arr)",
   "id": "c07505c528fad886",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:42:34.324607Z",
     "start_time": "2025-11-02T09:42:34.319021Z"
    }
   },
   "cell_type": "code",
   "source": "arr = np.random.randint(1,100,[10,10])",
   "id": "ec0beb21212365f0",
   "outputs": [],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:42:37.060916Z",
     "start_time": "2025-11-02T09:42:37.047401Z"
    }
   },
   "cell_type": "code",
   "source": "arr",
   "id": "e78e77a44335c6a0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[50, 23, 56, 74, 70, 83, 26, 63, 87, 54],\n",
       "       [43, 97, 89, 64, 29, 25, 70,  9, 26, 51],\n",
       "       [98, 24, 94, 18,  4, 30, 71, 58, 92,  2],\n",
       "       [ 9, 65,  8, 70, 25, 87, 29, 61,  6,  1],\n",
       "       [ 1, 99,  7, 84, 29, 47, 33, 92, 12, 44],\n",
       "       [ 4, 67,  4, 38, 64, 71, 57, 37, 66, 30],\n",
       "       [35, 14, 97,  6, 74, 18, 75, 24, 62, 90],\n",
       "       [23, 30, 84,  7, 14, 77, 95, 30, 65,  8],\n",
       "       [ 6, 28, 39, 71,  8, 31, 19, 53, 47, 28],\n",
       "       [79,  9, 33, 12, 37, 61, 30, 74, 44, 58]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.单位矩阵",
   "id": "1cbc7f375b5c9b80"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:45:05.922817Z",
     "start_time": "2025-11-02T09:45:05.906306Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "7b5f11fa91a31b6d",
   "outputs": [],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:45:26.146570Z",
     "start_time": "2025-11-02T09:45:26.109575Z"
    }
   },
   "cell_type": "code",
   "source": "E1 = np.eye(3)",
   "id": "edc1d4fe96df8032",
   "outputs": [],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:45:53.411218Z",
     "start_time": "2025-11-02T09:45:53.397707Z"
    }
   },
   "cell_type": "code",
   "source": "E2 = np.identity(3)",
   "id": "f7980bcdea6763e5",
   "outputs": [],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:45:54.431160Z",
     "start_time": "2025-11-02T09:45:54.415119Z"
    }
   },
   "cell_type": "code",
   "source": "E1",
   "id": "dc525713a5221215",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:45:59.274318Z",
     "start_time": "2025-11-02T09:45:59.270317Z"
    }
   },
   "cell_type": "code",
   "source": "E2",
   "id": "8665bdb7f9b2b1d0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 95
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:46:21.220896Z",
     "start_time": "2025-11-02T09:46:21.213941Z"
    }
   },
   "cell_type": "code",
   "source": "type(E1)",
   "id": "55965dcf595e8ac4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 96
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:46:26.353692Z",
     "start_time": "2025-11-02T09:46:26.341666Z"
    }
   },
   "cell_type": "code",
   "source": "type(E2)",
   "id": "f12ee0c40f76a389",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 97
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:46:32.933373Z",
     "start_time": "2025-11-02T09:46:32.925826Z"
    }
   },
   "cell_type": "code",
   "source": "np.shape(E1)",
   "id": "e7236169f879ea66",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 98
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:46:38.351088Z",
     "start_time": "2025-11-02T09:46:38.342003Z"
    }
   },
   "cell_type": "code",
   "source": "np.shape(E2)",
   "id": "ac9cff0a5118a801",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 99
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.对角矩阵",
   "id": "4a366b45f4f73778"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (1) 通过np.diag()创建对角矩阵",
   "id": "c914dd52e2ba767a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:49:19.064231Z",
     "start_time": "2025-11-02T09:49:19.054722Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "22c598d87a774da8",
   "outputs": [],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:49:26.459209Z",
     "start_time": "2025-11-02T09:49:26.446770Z"
    }
   },
   "cell_type": "code",
   "source": "a = [1,2,3,4,5,6]",
   "id": "cad3f61211a6cbd1",
   "outputs": [],
   "execution_count": 101
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:50:10.898990Z",
     "start_time": "2025-11-02T09:50:10.892480Z"
    }
   },
   "cell_type": "code",
   "source": "arr1 = np.diag(a)",
   "id": "8e679524b403f324",
   "outputs": [],
   "execution_count": 104
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:50:14.370726Z",
     "start_time": "2025-11-02T09:50:14.358218Z"
    }
   },
   "cell_type": "code",
   "source": "arr1",
   "id": "ab701c4581f6b004",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0, 0, 0],\n",
       "       [0, 2, 0, 0, 0, 0],\n",
       "       [0, 0, 3, 0, 0, 0],\n",
       "       [0, 0, 0, 4, 0, 0],\n",
       "       [0, 0, 0, 0, 5, 0],\n",
       "       [0, 0, 0, 0, 0, 6]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:50:35.933055Z",
     "start_time": "2025-11-02T09:50:35.917524Z"
    }
   },
   "cell_type": "code",
   "source": "arr2 = np.diag(arr1)  ##获取对角矩阵的对角上的矩阵",
   "id": "aad5763a43046377",
   "outputs": [],
   "execution_count": 106
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:50:37.640294Z",
     "start_time": "2025-11-02T09:50:37.631070Z"
    }
   },
   "cell_type": "code",
   "source": "arr2",
   "id": "6d15700c381356f5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 107
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:51:19.463657Z",
     "start_time": "2025-11-02T09:51:19.456140Z"
    }
   },
   "cell_type": "code",
   "source": "type(arr2)",
   "id": "12d4653305035ab5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:51:30.873729Z",
     "start_time": "2025-11-02T09:51:30.863992Z"
    }
   },
   "cell_type": "code",
   "source": "print(arr2.shape)",
   "id": "5248891155c73c6d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (2) 根据矩阵的对角对角线元素生成对角矩阵，只需要嵌套两个diag()函数即可",
   "id": "41d9fd7f2e1f3d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:57:43.080922Z",
     "start_time": "2025-11-02T09:57:43.064491Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "C = np.array(A)"
   ],
   "id": "d97fa30ba7185a0e",
   "outputs": [],
   "execution_count": 110
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:57:46.751358Z",
     "start_time": "2025-11-02T09:57:46.734234Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "337b6dc113e494e5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 111
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:58:16.327270Z",
     "start_time": "2025-11-02T09:58:16.313249Z"
    }
   },
   "cell_type": "code",
   "source": "arr = np.diag(np.diag(C))",
   "id": "e308c4bee811a6bd",
   "outputs": [],
   "execution_count": 112
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T09:58:20.275769Z",
     "start_time": "2025-11-02T09:58:20.267768Z"
    }
   },
   "cell_type": "code",
   "source": "arr",
   "id": "8a0c8d6d169318c3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [0, 5, 0],\n",
       "       [0, 0, 9]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 113
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (3) diag()函数作用于矩阵（不一定为方阵）时可以获得矩阵的对角线元素",
   "id": "368b8a6dd84a0532"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:01:13.093653Z",
     "start_time": "2025-11-02T10:01:13.088457Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "b073290a0e3f0a71",
   "outputs": [],
   "execution_count": 114
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:01:38.054305Z",
     "start_time": "2025-11-02T10:01:38.038520Z"
    }
   },
   "cell_type": "code",
   "source": "C = np.array([[1,2,3],[4,5,6]])",
   "id": "a3ae2db5d0fb1400",
   "outputs": [],
   "execution_count": 115
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:01:43.153608Z",
     "start_time": "2025-11-02T10:01:43.139477Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "dc66663e04d60d44",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 116
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:01:57.128444Z",
     "start_time": "2025-11-02T10:01:57.112608Z"
    }
   },
   "cell_type": "code",
   "source": "arr = np.diag(C)",
   "id": "dd1afef88f4e3d85",
   "outputs": [],
   "execution_count": 117
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:02:43.881825Z",
     "start_time": "2025-11-02T10:02:43.868438Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"获得矩阵C的对角线元素\",arr)",
   "id": "3016572d648dbb49",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获得矩阵C的对角线元素 [1 5]\n"
     ]
    }
   ],
   "execution_count": 118
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 5.上三角矩阵",
   "id": "83e9680e236b54f0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:19:14.235300Z",
     "start_time": "2025-11-02T10:19:14.232728Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = np.array([[1,2,3,2],[4,5,6,3],[7,8,9,4],[3,5,6,8]])"
   ],
   "id": "30af82182efc6f26",
   "outputs": [],
   "execution_count": 122
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:19:24.363864Z",
     "start_time": "2025-11-02T10:19:24.353325Z"
    }
   },
   "cell_type": "code",
   "source": "upper_A = np.triu(A,0)",
   "id": "d81f4a66c25acd5",
   "outputs": [],
   "execution_count": 125
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:19:24.911652Z",
     "start_time": "2025-11-02T10:19:24.895035Z"
    }
   },
   "cell_type": "code",
   "source": "upper_A",
   "id": "55c49b089a46e4f0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 2],\n",
       "       [0, 5, 6, 3],\n",
       "       [0, 0, 9, 4],\n",
       "       [0, 0, 0, 8]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 126
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.下三角矩阵",
   "id": "3fb439cc8671d140"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": 127,
   "source": "lower_A = np.tril(A,0)",
   "id": "8341869cde8e1da2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0],\n",
       "       [4, 5, 0, 0],\n",
       "       [7, 8, 9, 0],\n",
       "       [3, 5, 6, 8]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 128,
   "source": "lower_A",
   "id": "947528db638f2188"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 7.对称矩阵",
   "id": "b7bd8b7f9c6a344f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:29:35.751206Z",
     "start_time": "2025-11-02T10:29:35.743440Z"
    }
   },
   "cell_type": "code",
   "source": "### 以转置矩阵为例子来说明对称矩阵",
   "id": "e5f7cf50aaf24d1c",
   "outputs": [],
   "execution_count": 132
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (1) np.transpose()和T属性来实现",
   "id": "9547c4a89690381d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:32:56.249909Z",
     "start_time": "2025-11-02T10:32:56.233800Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = [[2,6,10],[1,-2,9]]\n",
    "B = np.array(A) ### Note: 不能直接对list类型进行T属性。\n",
    "print(\"(1) np.transpose()：\\n\",np.transpose(B))"
   ],
   "id": "f44dc791340f1234",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1) np.transpose()：\n",
      " [[ 2  1]\n",
      " [ 6 -2]\n",
      " [10  9]]\n"
     ]
    }
   ],
   "execution_count": 135
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:33:28.011591Z",
     "start_time": "2025-11-02T10:33:28.008081Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"T属性来实现矩阵的转置：\\n\",B.T)",
   "id": "1a7a73a3f220029e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T属性来实现矩阵的转置：\n",
      " [[ 2  1]\n",
      " [ 6 -2]\n",
      " [10  9]]\n"
     ]
    }
   ],
   "execution_count": 136
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "### Note: 不能直接对list类型进行T属性。",
   "id": "92e2f1512fa1137b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### （2）矩阵转置的性质",
   "id": "8f30ce49dc5c834a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:39:12.764554Z",
     "start_time": "2025-11-02T10:39:12.751051Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = [[1,2,3],[4,5,6]]\n",
    "B = [[7,8],[9,10],[11,12]]"
   ],
   "id": "68a554b56857e284",
   "outputs": [],
   "execution_count": 137
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:39:17.930557Z",
     "start_time": "2025-11-02T10:39:17.922023Z"
    }
   },
   "cell_type": "code",
   "source": "C = np.array(A)",
   "id": "c6694f2d1f2203f2",
   "outputs": [],
   "execution_count": 138
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:39:21.256135Z",
     "start_time": "2025-11-02T10:39:21.241547Z"
    }
   },
   "cell_type": "code",
   "source": "D = np.array(B)",
   "id": "e0de55897c6d7e01",
   "outputs": [],
   "execution_count": 139
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:40:35.731107Z",
     "start_time": "2025-11-02T10:40:35.726620Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "8e11ba6fb9b36936",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 144
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:40:42.993998Z",
     "start_time": "2025-11-02T10:40:42.985976Z"
    }
   },
   "cell_type": "code",
   "source": "D",
   "id": "6d6a995118e07a2c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7,  8],\n",
       "       [ 9, 10],\n",
       "       [11, 12]])"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 145
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:40:53.170206Z",
     "start_time": "2025-11-02T10:40:53.151862Z"
    }
   },
   "cell_type": "code",
   "source": "C.dot(D)",
   "id": "a57600539c8dc656",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 58,  64],\n",
       "       [139, 154]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 146
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:41:13.292523Z",
     "start_time": "2025-11-02T10:41:13.288183Z"
    }
   },
   "cell_type": "code",
   "source": "D.T.dot(C.T)",
   "id": "df6d5bbbe09046c0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 58, 139],\n",
       "       [ 64, 154]])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 147
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (3)利用矩阵的转置，可以创建对称矩阵",
   "id": "a6cdfe4b8b96cb8a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 利用randint()函数创建一个随机整数的方阵",
   "id": "833c39e86b6608c6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:45:26.650154Z",
     "start_time": "2025-11-02T10:45:26.635641Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.randint(1,16,[3,3])\n",
    "arr1"
   ],
   "id": "3be45d5c653ebd4a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  5,  4],\n",
       "       [ 7, 15,  1],\n",
       "       [12, 13, 15]])"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 148
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:45:55.833366Z",
     "start_time": "2025-11-02T10:45:55.816797Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.triu(arr1)\n",
    "arr2"
   ],
   "id": "6f8021e5e75380fc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  5,  4],\n",
       "       [ 0, 15,  1],\n",
       "       [ 0,  0, 15]])"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 149
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:46:35.886770Z",
     "start_time": "2025-11-02T10:46:35.860483Z"
    }
   },
   "cell_type": "code",
   "source": "arr2 += arr2.T - np.diag(np.diag(arr2))",
   "id": "fd1e6b0dfe2c16f2",
   "outputs": [],
   "execution_count": 150
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:46:38.416443Z",
     "start_time": "2025-11-02T10:46:38.402544Z"
    }
   },
   "cell_type": "code",
   "source": "arr2",
   "id": "ee3e474435ee2c44",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  5,  4],\n",
       "       [ 5, 15,  1],\n",
       "       [ 4,  1, 15]])"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 151
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### (4) 判断A是否为对称矩阵",
   "id": "3a9e94974d3e2ad5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:51:02.371054Z",
     "start_time": "2025-11-02T10:51:02.363547Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([[1,2],[3,4]])\n",
    "arr2 = np.array([[1,2,3],[2,4,5],[3,5,6]])"
   ],
   "id": "e2ac9e4cf9691f0f",
   "outputs": [],
   "execution_count": 154
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:51:23.008210Z",
     "start_time": "2025-11-02T10:51:23.003699Z"
    }
   },
   "cell_type": "code",
   "source": "# 判断arr1和arr2是否为对称矩阵",
   "id": "a3d802a5ae3ded71",
   "outputs": [],
   "execution_count": 155
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:51:40.485960Z",
     "start_time": "2025-11-02T10:51:40.465887Z"
    }
   },
   "cell_type": "code",
   "source": "np.allclose(arr1,arr1.T)",
   "id": "e8693f5935e5dc9f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 156
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:54:09.652719Z",
     "start_time": "2025-11-02T10:54:09.640261Z"
    }
   },
   "cell_type": "code",
   "source": "np.allclose(arr2,arr2.T)",
   "id": "8417a11f26a0a216",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 157
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### （5）利用矩阵的转置，可以用行列向量生成列向量",
   "id": "dbd3125f64bd2832"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:57:25.704747Z",
     "start_time": "2025-11-02T10:57:25.698528Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = [[1,2,3,4,5]]\n",
    "B = np.array(A)\n",
    "C = B.T"
   ],
   "id": "5c004e83e9b69c99",
   "outputs": [],
   "execution_count": 158
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:57:27.026959Z",
     "start_time": "2025-11-02T10:57:27.010951Z"
    }
   },
   "cell_type": "code",
   "source": "A",
   "id": "2b04164a88a3b38",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3, 4, 5]]"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 159
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:57:31.162157Z",
     "start_time": "2025-11-02T10:57:31.152646Z"
    }
   },
   "cell_type": "code",
   "source": "B",
   "id": "e238e3d02173ebb7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5]])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 160
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:57:35.014115Z",
     "start_time": "2025-11-02T10:57:35.001748Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "a53ea0a163c002ac",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5]])"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 161
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:58:42.089506Z",
     "start_time": "2025-11-02T10:58:42.073225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = [1,2,3,4,5]\n",
    "B = np.array(A)\n",
    "C = B.T\n",
    "print(B)"
   ],
   "id": "3690b13be763be85",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "execution_count": 162
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:58:45.353408Z",
     "start_time": "2025-11-02T10:58:45.339108Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "75cf70d80cbcfa3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:59:00.984545Z",
     "start_time": "2025-11-02T10:59:00.978036Z"
    }
   },
   "cell_type": "code",
   "source": "type(B)",
   "id": "d6e91d9001b02b7d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 164
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T10:59:04.648341Z",
     "start_time": "2025-11-02T10:59:04.634320Z"
    }
   },
   "cell_type": "code",
   "source": "type(C)",
   "id": "e804d7e63aefd4dc",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 165
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 8.同型矩阵",
   "id": "caf20efebed8c991"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": 129,
   "source": [
    "import numpy as np\n",
    "A = [3,4]\n",
    "B = [4,3]"
   ],
   "id": "4f179fc37cb8143a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": 130,
   "source": "arr1 = np.random.randint(3,9,A)",
   "id": "f0ab7afc3fd94de8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 5, 7, 6],\n",
       "       [8, 8, 8, 8],\n",
       "       [3, 6, 5, 5]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 131,
   "source": "arr1",
   "id": "769bac634858e480"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 9.矩阵相等",
   "id": "e20fd86cabd65129"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T11:03:51.650180Z",
     "start_time": "2025-11-02T11:03:51.637145Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])\n",
    "B = np.array([[2,3,5,6]])\n",
    "C = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])"
   ],
   "id": "e52ca74015f9c213",
   "outputs": [],
   "execution_count": 170
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T11:03:52.341840Z",
     "start_time": "2025-11-02T11:03:52.337337Z"
    }
   },
   "cell_type": "code",
   "source": "print(np.allclose(A,B))",
   "id": "49f8e519685ba7b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "execution_count": 171
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T11:03:12.438514Z",
     "start_time": "2025-11-02T11:03:12.424480Z"
    }
   },
   "cell_type": "code",
   "source": "print(np.allclose(A,C))",
   "id": "d005c59e4f8e5311",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "execution_count": 169
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 矩阵的基本操作",
   "id": "c41085cee3f18d0a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1.矩阵的加减法运算",
   "id": "ab898af579015d4e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:55:55.014708Z",
     "start_time": "2025-11-03T22:55:54.998672Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "A=[[1,2,3],[3,2,1]]\n",
    "B=[[6,8,12],[10,5,12]]"
   ],
   "id": "5a298e1fb0adfd31",
   "outputs": [],
   "execution_count": 173
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:56:00.422119Z",
     "start_time": "2025-11-03T22:56:00.411107Z"
    }
   },
   "cell_type": "code",
   "source": "C=A+B",
   "id": "9be914f999d4d883",
   "outputs": [],
   "execution_count": 174
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:56:02.137284Z",
     "start_time": "2025-11-03T22:56:02.133287Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "20f99c66916210ab",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [3, 2, 1], [6, 8, 12], [10, 5, 12]]"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 175
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:56:29.937955Z",
     "start_time": "2025-11-03T22:56:29.929156Z"
    }
   },
   "cell_type": "code",
   "source": "C1 = np.array(A)",
   "id": "2aad17af32a5da73",
   "outputs": [],
   "execution_count": 176
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:56:36.551843Z",
     "start_time": "2025-11-03T22:56:36.534085Z"
    }
   },
   "cell_type": "code",
   "source": "D1 = np.array(B)",
   "id": "e63680989cbb5677",
   "outputs": [],
   "execution_count": 177
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:56:57.747380Z",
     "start_time": "2025-11-03T22:56:57.736457Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C1+D1: \\n\",C1+D1)",
   "id": "1aaca1579646b0eb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C1+D1: \n",
      " [[ 7 10 15]\n",
      " [13  7 13]]\n"
     ]
    }
   ],
   "execution_count": 178
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:57:25.040389Z",
     "start_time": "2025-11-03T22:57:25.025285Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"C1-D1: \\n\",C1-D1)",
   "id": "67ecf5d79b519090",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C1-D1: \n",
      " [[ -5  -6  -9]\n",
      " [ -7  -3 -11]]\n"
     ]
    }
   ],
   "execution_count": 179
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2.矩阵的数乘运算",
   "id": "7f580d362d473e9e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-03T22:58:57.548664Z",
     "start_time": "2025-11-03T22:58:57.545661Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"n*C1= \\n\",5*C1)",
   "id": "5f4cdc1c1455ff80",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n*C1= \n",
      " [[ 5 10 15]\n",
      " [15 10  5]]\n"
     ]
    }
   ],
   "execution_count": 180
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. 矩阵的相乘",
   "id": "bd9bce75f29060b9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T09:26:42.633059Z",
     "start_time": "2025-11-04T09:26:42.630621Z"
    }
   },
   "cell_type": "code",
   "source": "A = [[1,2],[3,4]]",
   "id": "50c882e2c58c4bbf",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T09:26:55.152256Z",
     "start_time": "2025-11-04T09:26:55.150025Z"
    }
   },
   "cell_type": "code",
   "source": "B=[[2,3],[4,5]]",
   "id": "3ec4e5b3d49c1e50",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T09:27:54.746707Z",
     "start_time": "2025-11-04T09:27:54.618810Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "C = np.array(A)*np.array(B)"
   ],
   "id": "484ea142a0417d23",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T09:27:56.847842Z",
     "start_time": "2025-11-04T09:27:56.844202Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "bbd62141990111a8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "import numpy as np",
   "id": "76dd05a3d5716468"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T23:05:07.913525Z",
     "start_time": "2025-11-04T23:05:07.899657Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A = [[1,2,3],[4,5,6]]\n",
    "B = [[4,5,6],[7,8,9],[3,5,6]]"
   ],
   "id": "2acaee7d12fd0096",
   "outputs": [],
   "execution_count": 182
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T23:05:08.662997Z",
     "start_time": "2025-11-04T23:05:08.648980Z"
    }
   },
   "cell_type": "code",
   "source": "C = np.dot(A,B)",
   "id": "4c5f0bf53cf7650b",
   "outputs": [],
   "execution_count": 183
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T23:05:16.316058Z",
     "start_time": "2025-11-04T23:05:16.305553Z"
    }
   },
   "cell_type": "code",
   "source": "C",
   "id": "55094e76af12c1cd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 27,  36,  42],\n",
       "       [ 69,  90, 105]])"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 184
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T23:06:16.600679Z",
     "start_time": "2025-11-04T23:06:16.587176Z"
    }
   },
   "cell_type": "code",
   "source": [
    "## One_vector\n",
    "one_v1 = np.array([1,2,3])\n",
    "one_v2 = np.array([4,5,6])\n",
    "one_mul = np.dot(one_v1,one_v2)"
   ],
   "id": "e166b4f543c9864d",
   "outputs": [],
   "execution_count": 185
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T23:06:20.317546Z",
     "start_time": "2025-11-04T23:06:20.302039Z"
    }
   },
   "cell_type": "code",
   "source": "print(one_mul)",
   "id": "c166da93bab1d4a7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32\n"
     ]
    }
   ],
   "execution_count": 186
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "b5cfebee6c48efdf"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
